<?php

namespace app\user\controller;

use app\model\UserInfo as ModelUserInfo;
use app\user\logic\SMS as userLogicSMS;
use app\model\UserCertification as ModelUserCertification;
use app\model\UserPassword as ModelUserPassword;
use app\logic\sms\SMS AS LogicSMS;
use app\logic\IdentityCard as LogicIdentityCard;



/**
 * 用户相关类
 * Class Common
 * @package app\user\controller
 */
class Member extends Common
{
    protected $user_id;

    public function _initialize()
    {
        parent::_initialize();
        $obj = new \app\logic\Passport($this->str_module_name);
        $this->user_id = $obj->_checkPassport($this->_getContentParams('passport'), $this->_getParams('platform'));
        unset($obj);
    }

    /**
     * 用户信息修改 \n
     * URI : /user.info.update
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  type          | string | 必填  | 类型（nickname:昵称，headPortrait:头像）
     *  nickname      | string | 选填  | 昵称 (type=nickname)
     *  headPortrait  | string | 选填  | 头像 (type=headPortrait)
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     */
    public function updateInfo()
    {
        $str_type = $this->_getContentParams('type');

        $data = [];
        switch ($str_type) {
            case 'nickname' :
                //昵称
                $data['nickname'] = $this->_getContentParams('nickname');
                _toCompare(empty($data['nickname']), '==', true, 12006);
                break;
            case 'headPortrait':
                //头像
                $data['head_portrait'] = $this->_getContentParams('headPortrait');
                _toCompare(empty($data['head_portrait']), '==', true, 12007);
                break;
            default:
                _returnError(10014);
        }

        //修改数据
        $res = ModelUserInfo::getInstance()->toUpdate(['user_id' => $this->user_id], $data);

        if ($res) _returnData();
        _returnError(10011);
    }

    /**
     * 已登录重置密码 \n
     * URI : /message.reset.password
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  password      | string | 必填  | 旧密码
     *  newPassword   | string | 选填  | 新密码
     *  newPasswordTo | string | 选填  | 再次新密码
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     */
    public function resetPassword()
    {
        $str_password = $this->_getContentParams('password'); // 旧密码
        _toValidate('login_pwd', $str_password, 19027);
        $str_new_password = $this->_getContentParams('newPassword'); //新密码
        _toValidate('login_pwd', $str_new_password, 19025);
        $str_new_password_to = $this->_getContentParams('newPasswordTo'); //再次密码
        _toCompare($str_new_password_to, '!=', $str_new_password, 19026);
        $arr_pwd_info = ModelUserPassword::getInstance()->toFind(['user_id' => $this->user_id], 'password,pwd_key');
        _toCompare(passwordEncryption($str_password, $arr_pwd_info['pwd_key']), '!=', $arr_pwd_info['password'], 19028);
        ModelUserPassword::getInstance()->toUpdate(['user_id' => $this->user_id], ['password' => passwordEncryption($str_new_password, $arr_pwd_info['pwd_key'])]);
        _returnData();
    }

    /**
     * 已登录且第一次设置密码 \n
     * URI : /member.set.password
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  newPassword   | string | 选填  | 新密码
     *
     * @return
     *  name  | type | description
     * -------|------|-------------
     *  ----- | ---- | 无
     */
    public function setUserPassword()
    {
        $str_new_password = $this->_getContentParams('newPassword'); //新密码
        _toValidate('login_pwd', $str_new_password, 19025);
        $arr_pwd_info = ModelUserPassword::getInstance()->toFind(['user_id' => $this->user_id], 'password,pwd_key');
        _toCompare(passwordEncryption($str_new_password, $arr_pwd_info['pwd_key']), '!=', $arr_pwd_info['password'], 19028);
        ModelUserPassword::getInstance()->toUpdate(['user_id' => $this->user_id], ['password' => passwordEncryption($str_new_password, $arr_pwd_info['pwd_key'])]);
        _returnData();
    }

    /**
     * 实名审核 \n
     * URI : /user.auth
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  cardFace      | string | 必填  | 身份证正面
     *  cardBack      | string | 必填  | 身份证反面
     *  realName      | string | 必填  | 真实姓名
     *  idCard        | string | 必填  | 身份证号
     *  mobile        | string | 必填  | 手机号
     *  code          | string | 必填  | 验证码
     *  passKey       | string | 必填  | 验证码密钥
     *  startTerm     | string | 必填  | 有效起始日期
     *  endTerm       | string | 必填  | 有效结束日期
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------  | ------ | 无
     */
    public function idCardAuth()
    {
        $data['idcard_front'] = $this->_getContentParams('idCardFront'); //身份证正面
        _toCompare(empty($data['idcard_front']), '==', true, 12008);

        $data['idcard_back'] = $this->_getContentParams('idCardBack'); //身份证反面
        _toCompare(empty($data['idcard_back']), '==', true, 12009);

        $data['real_name'] = $this->_getContentParams('realName'); //真实姓名
        _toCompare(empty($data['real_name']), '==', true, 12010);

        $data['id_number'] = $this->_getContentParams('idCard'); //身份证号
        _toValidate('id_card', $data['id_number'], 12011);

        $data['start_term'] = $this->_getContentParams('startTerm'); //有效起始日期
        _toValidate('date', $data['start_term'], 11006); //有效期验证

        $data['end_term'] = $this->_getContentParams('endTerm'); //有效结束日期
        _toValidate('date', $data['end_term'], 11006);

        $mobile = ModelUserInfo::getInstance()->toValue(['user_id'=>$this->user_id],'mobile');// $this->_getContentParams('mobile'); //手机号
        _toValidate('mobile', $mobile, 11002);

        $code = $this->_getContentParams('code'); //验证码
        _toValidate('verify_code', $code, 11004);
        $check_result = LogicSMS::getInstance()->verifySMS('auth', $mobile, $this->_getContentParams('passKey'), $code, $this->str_module_name);// 验证验证码
        _toCompare($check_result, '===', false, 11005);

        $data['is_auth'] = 1;

        $bool_res = LogicIdentityCard::getInstance()->aliYunCheckIdCard($data['real_name'], $data['id_number']);
        _toCompare($bool_res, '===', false, 15009);
        //修改数据
        $where['user_id'] = $this->user_id;

        $res = ModelUserInfo::getInstance()->toUpdate($where,$data);

        if ($res) _returnData();
        _returnError(10010);
    }

    /**
     * 上传图片
     * URI:/user/img.upload
     */
    public function uploadImg()
    {
        $obj = new \app\logic\UploadImg($this->str_module_name);
        $str_type = $this->_getContentParams('type');
        $obj->uploadImg($str_type);
    }
}
